console.log(1);
var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
var flag = true
$('.inputss input').each(function (index, i) {
    $(i).bind("input propertychange", function () {
        if ($('#register_mail').val() != '' && flag == true) {
            $('#mailcode').css({
                'background': '#00A862',
                'border': '0',
                'color': '#fff',
                'cursor': 'pointer'
            })
            $('#mailcode').removeAttr("disabled");
        } else {
            $('#mailcode').css({
                'background': '#f7f7f7',
                'border-color': '#f7f7f7',
                'color': 'rgba(0, 0, 0, 0.38)',
                'border': '1px solid #f7f7f7',
                'cursor': 'not-allowed'
            })
            $('#mailcode').attr("disabled", "disabled");
        }
        if ($('#register_mail').val() != '' && $('#register_mailcode').val() != '' && $('#register_password').val() != '') {
            $('#register_btn').css({
                'background': '#00A862',
                'border': '0',
                'color': '#fff',
                'cursor': 'pointer'
            })
            $('#register_btn').removeAttr("disabled");

        } else {
            $('#register_btn').css({
                'background': '#f7f7f7',
                'border -color': '#f7f7f7',
                'color': 'rgba(0, 0, 0, 0.38)',
                'cursor': 'not-allowed'
            })
            $('#register_btn').attr("disabled", "disabled");
        }
    })
})
//点击发送验证码

$('#mailcode').click(function () {
    if (reg.test($('#register_mail').val())) { //邮箱格式正确发送验证码
        if (flag) {
            flag = false //点击之后120s后才能点击
            $('#mailcode').css({
                'background': '#f7f7f7',
                'border-color': '#f7f7f7',
                'color': 'rgba(0, 0, 0, 0.38)',
                'cursor': 'not-allowed',
                'border': '1px solid #f7f7f7'
            })

            var t = 120
            $('#mailcode').text(`${t}s后重新获取`)
            var mail = $("#register_mail").val();
            $.ajax({ //发送验证码请求
                method: "POST",
                url: "/register/code",

                data: {
                    mail
                },
                success: (data) => {
                    if (data.code == 200) {
                        alert(data.msg);
                    } else if (data.code == 500) {
                        alert(data.msg);
                    }
                }
            })
            var tid = setInterval(() => {
                t--
                $('#mailcode').text(`${t}s后重新获取`)
                if (t == 0) {
                    clearInterval(tid)
                    $('#mailcode').css({
                        'background': '#00A862',
                        'border': '0',
                        'color': '#fff',
                        'cursor': 'pointer'
                    })
                    $('#mailcode').text(`获取验证码`)
                    flag = true
                }
            }, 1000);
        }
    } else {
        alert("邮箱格式不正确");
    }
    // console.log(1);


})

//点击 注册
$('#register_btn').click(function () {
    if (reg.test($('#register_mail').val())) {
        $.ajax({
            method: "post",
            url: "/register",
            data: {
                mail: $("#register_mail").val(),
                password: $("#register_password").val(),
                mailcode: $("#register_mailcode").val()
            },
            success: function (data) {
                alert(data.msg)
                if (data.code == 200) {
                    location.href = '/'
                }
            },
            fail: function (err) {
                console.log(err);
            }
        })
    } else {
        alert("邮箱格式不正确");
    }


})